﻿Public Class ctlEditor

    Dim FindIsOpen As Boolean = False
    Dim SDialog As dlgSearchReplace

    'The File Associated With This Editor
    Private FName As String = ""
    Private mTag As String = ""
    ' Control's Name
    'Private ctlName As String = "ctlEditor"

    Private UnSavedWork As Boolean = False

    'Public Accessor for reading the FName variable
    ReadOnly Property FileName As String
        Get
            Return FName
        End Get
    End Property

    Public Property WorkUnsaved As Boolean
        Set(ByVal value As Boolean)
            UnSavedWork = value
        End Set
        Get
            Return UnSavedWork
        End Get
    End Property

    Public Sub New(ByVal FullFileName As String, ByVal ctlNameString As String, Optional ByVal txtFName As String = "")
        ' this call is required by the designer.
        InitializeComponent()
        'add any initialization after the initializecomponent() call.

        ' Set the fname variable at creation
        FName = FullFileName

        If txtFName <> "" Then
            mTag = txtFName
        Else
            mTag = FName
        End If

        ' Set the Control's Name at Creation
        Me.Name = ctlNameString
    End Sub

    Private Sub ctlEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SDialog = New dlgSearchReplace(rtbEditBox)
        Try
            Me.Tag = mTag
            Me.rtbEditBox.LoadFile(FName, RichTextBoxStreamType.PlainText)
            UnSavedWork = False
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub rtbEditBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbEditBox.TextChanged
        Me.Parent.Invalidate()
    End Sub

    Public Sub SaveFile()
        Dim finfo As New System.IO.FileInfo(FName)
        If finfo.Extension = ".rbf" Then
            Me.rtbEditBox.SaveFile(FName, RichTextBoxStreamType.PlainText)
            FileOps.TXTtoRBF(FName, Tag)
        Else
            Me.rtbEditBox.SaveFile(FName, RichTextBoxStreamType.PlainText)
        End If
        UnSavedWork = False
        ' Invalidate to Force Repaint.
        Me.Parent.Invalidate()
    End Sub

End Class
